# -*- coding:utf-8 -*-
from TestBase.browser_action import Base
from TestBase.webdriver_package import *


class SearchPage(Base):

    def __init__(self, case_instance):
        super(SearchPage, self).__init__(case_instance, csv_file="search_page.csv")

    """
       【 元 素 定 位 】
    """

    # locator_data = csv(file=project_path()+"/Project/pro_demo_1/page_locator/search_page.csv", data_type="page")
    #
    # # '出发城市'输入框
    # # @property
    # def search_leave(self):
    #     # return self.find_ele(By.ID, "departCityName")
    #     return self.find_ele(self.locator_data[get_func_name()][0], self.locator_data[get_func_name()][1])
    #
    # # '到达城市'输入框
    # # @property
    # def search_arrive(self):
    #     # return self.find_ele(By.ID, "arriveCityName")
    #     return self.find_ele(self.locator_data[get_func_name()][0], self.locator_data[get_func_name()][1])
    #
    # # '出发时间'控件
    # # @property
    # def search_date(self):
    #     # return self.find_ele(By.ID, "departDate")
    #     return self.find_ele(self.locator_data[get_func_name()][0], self.locator_data[get_func_name()][1])
    #
    # # '单程'tab控件
    # # @property
    # def search_current(self):
    #     # return self.find_ele(By.CSS_SELECTOR, "#search_type > li.current")
    #     # return self.find_ele(By.XPATH, "//*[@id='searchBoxTemplete']/div/div[1]/div[2]/ul/li[1]")
    #     return self.find_ele(self.locator_data[get_func_name()][0], self.locator_data[get_func_name()][1])
    #
    # # '开始搜索'按钮
    # # @property
    # def search_btn(self):
    #     # return self.find_ele(By.CLASS_NAME, "searchbtn")
    #     return self.find_ele(self.locator_data[get_func_name()][0], self.locator_data[get_func_name()][1])

    """
        【 页 面 功 能 】
    """

    def search_train(self):
        # 截屏，断言内容是否存在（ 当前页面 ）
        self.screenshot(img_no=1)
        self.assert_content(content=self.data("assert_home_key"), error_msg="首页跳转失败")

        old_handle = self.open_new_window()
        time.sleep(2)
        self.close()
        time.sleep(2)
        self.driver.switch_to.window(old_handle)

        # 输入'出发城市'
        self.search_leave().clear()
        self.search_leave().send_keys(self.data("leave"))
        # search_leave_id = self.locator_data["search_leave"][1]
        # self.js(f"document.getElementById(\"{search_leave_id}\").value='{self.params_data['leave']}'")
        time.sleep(2)

        # 输入'到达城市'
        self.search_arrive().clear()
        self.search_arrive().send_keys(self.data("arrive"))
        # search_arrive_id = self.locator_data["search_arrive"][1]
        # self.js(f"document.getElementById(\"{search_arrive_id}\").value='{params_data['arrive']}'")
        time.sleep(2)

        # 输入'出发时间'（ 使用js：先去掉readonly属性，再赋值 ）
        self.js_del_attr(ele=self.search_date(), attr_name="readonly")
        # 方式一（有问题：点击'单程'后 时间被还原了）
        # self.js_update_value(ele=self.search_date(), value=self.params_data["leave_date"])
        # 方式二
        self.search_date().clear()
        self.search_date().send_keys(self.data("leave_date"))
        time.sleep(3)

        # 点击'单程'tab
        self.search_current().click()
        time.sleep(3)

        # 单击'开始搜索'按钮
        self.screenshot(img_no=2)
        self.search_btn().click()
        time.sleep(4)

        # 截屏，断言内容是否存在（ 跳转页面 ）
        self.screenshot(img_no=3)
        self.assert_content(content=self.data("assert_detail_key"), error_msg="车票信息页跳转失败")
